﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImageModify
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "JPEG Compressed Image (*.jpg|*.jpg" + "|GIF Image(*.gif|*.gif" + "|Bitmap Image(*.bmp|*.bmp";
            openFileDialog1.Multiselect = false;
            openFileDialog1.FilterIndex = 1;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Bitmap tmpImage = new Bitmap(openFileDialog1.FileName);
                pictureBox1.Image = ImageCropResize((Image)tmpImage, 640, 360);
            }
            
        }

        private Image ImageCropResize(Image image, int width, int height)
        {
            
            Bitmap bmpImage = new Bitmap(image);
            Image final_image = image;
            //crop
            //specify the crop area
            Rectangle cropArea = new Rectangle();
            cropArea.Width = image.Width;
            cropArea.Height = (int)(cropArea.Width * 9 / 16);
            int location_y = (int)(image.Height / 2 - cropArea.Height/2);
            cropArea.Location = new Point(0,location_y);
            if (location_y < 0)
            {
                cropArea.Height = image.Height;
                cropArea.Width = (int)(cropArea.Height * 16 / 9);
                cropArea.Location = new Point((int)(image.Width / 2 - cropArea.Width / 2), 0);
            }
            //crop the image  get the bmpCrop
            Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
            //resize
            final_image = resizeImage((Image)bmpCrop, new Size(width, height));
            return final_image;
        }
        private Image resizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;
            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;
            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap bmpImage = new Bitmap(destWidth, destHeight);
            Graphics graphics = Graphics.FromImage((Image)bmpImage);
            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            graphics.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            graphics.Dispose();

            return (Image)bmpImage;
        }
    }
}
